公司地址:茂名市人民南路新村大院22號101
電話:13592986386
發(fā)布時(shí)間:2024/9/3 21:05:44
于是我就以我原有的思維認(rèn)為,這b肯定是我自己的代碼寫錯(cuò)了!于是,開始了我那痛苦的一天,我代碼一個(gè)一個(gè)排查,最后快崩潰的時(shí)候,上網(wǎng)查了一下,原因居然是:
然后我就想把flask的版本降低,但是想了想,不對呀,這樣更加吃虧了呀,我很多插件是基于flask的2.3.2版本的呀,很難受那時(shí)候,然后吃了個(gè)飯,想了想,要不修改一下源代碼?說干就干,開始了我一下午的網(wǎng)絡(luò)查找答案的過程,
最后還是靠chatgpt幫我修改了源代碼,功夫不負(fù)有心人,最后成功解決。!
from bson import json_util
from json import JSONEncoder
from mongoengine.base import BaseDocument
from mongoengine.queryset import QuerySet
class MongoEngineJSONEncoder(JSONEncoder):
"""
A JSONEncoder which provides serialization of MongoEngine
documents and queryset objects.
"""
def default(self, obj):
if isinstance(obj, BaseDocument):
return json_util._json_convert(obj.to_mongo())
elif isinstance(obj, QuerySet):
return json_util._json_convert(obj.as_pymongo())
return super().default(obj)
def override_json_encoder(app):
"""
A function to dynamically create a new MongoEngineJSONEncoder class
based upon a custom base class.
This function allows us to combine MongoEngine serialization with
any changes to Flask's JSONEncoder which a user may have made
prior to calling init_app.
NOTE: This does not cover situations where users override
an instance's json_encoder after calling init_app.
"""
app.json_encoder = MongoEngineJSONEncoder